import os
import sys
import fnmatch

# remove a file list in file list
def remove_file_list_in_list(slst, dlst) :
	for u in dlst :
		slst.remove(os.path.abspath(u))

# remove a list in list
def remove_list_in_list(slst, dlst) :
	for u in dlst :
		slst.remove(u)
        
# env
CC_TOOL = ARGUMENTS.get('build_cc', os.environ.get('CC'))
CXX_TOOL = ARGUMENTS.get('build_cxx', os.environ.get('CXX'))
AR_TOOL = ARGUMENTS.get('build_ar', os.environ.get('AR'))
if CC_TOOL == "" or CC_TOOL == None or CXX_TOOL == "" or CXX_TOOL == None :
	print("use default build environment")
	env = Environment()
else :
	print("use custom build environment")
	env = Environment(CC = CC_TOOL, CXX = CXX_TOOL)
	if AR_TOOL != "" and AR_TOOL != None : env.Replace(AR = AR_TOOL)

# base_paths
LOCAL_DIR = sys.path[0]
SRC_PATH 		= LOCAL_DIR + '/../'

# src 
source_files = [
	SRC_PATH + '/argparse.cpp',
]

source_files.extend(Split(ARGUMENTS.get('source_files', [])))
remove_file_list_in_list(source_files, Split(ARGUMENTS.get('exclude_source_files', [])))

# include
source_file_paths = [
	SRC_PATH ,
]

source_file_paths.extend(Split(ARGUMENTS.get('source_file_paths', [])))
remove_file_list_in_list(source_file_paths, Split(ARGUMENTS.get('exclude_source_file_paths', [])))

# build flags
build_flags = [
	'-std=c++11',
]

build_flags.extend(Split(ARGUMENTS.get('build_flags', [])))
remove_list_in_list(build_flags, Split(ARGUMENTS.get('exclude_build_flags', [])))

# link
libs = []
libs.extend(Split(ARGUMENTS.get('libs', [])))
remove_list_in_list(libs, Split(ARGUMENTS.get('exclude_libs', [])))

# link paths
lib_paths = []
lib_paths.extend(Split(ARGUMENTS.get('lib_paths', [])))
remove_list_in_list(lib_paths, Split(ARGUMENTS.get('exclude_lib_paths', [])))

#link flags
link_flags = []
link_flags.extend(Split(ARGUMENTS.get('link_flags', [])))
remove_list_in_list(link_flags, Split(ARGUMENTS.get('exclude_link_flags', [])))

# obj
obj_path = ARGUMENTS.get('obj_path', 'objs')
objs = []
for u in source_files :
        objs.append(env.Object(obj_path + '/' + os.path.basename(u) + '.o', u,
		CPPPATH = source_file_paths,
		CCFLAGS = build_flags,
		))

# all
bin = ARGUMENTS.get('bin', 'oaryarg-utils')
env.StaticLibrary(
	bin,
	source = objs,
	CPPPATH = source_file_paths,
	CCFLAGS = build_flags,
	CPPFLAGS = build_flags,
	LIBS = libs,
	LIBPATH = lib_paths,
	LINKFLAGS= link_flags,
)
